package com.my.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableJpaRepositories(value = "com.my" ,entityManagerFactoryRef = "EntityManagerFactory")
public class jpaConfig {

  private String hibernate_dialect="org.hibernate.dialect.MySQL5Dialect";
  private String hibernate_show_sql="true";
  private String hibernate_format_sql="true";
  private String hibernate_hbm2ddl_auto="update";


  @Autowired
  DataSource dataSource;

  @Bean
  public  HibernateJpaVendorAdapter hibernateJpaVendorAdapter(){
HibernateJpaVendorAdapter hibernateJpaVendorAdapter=new HibernateJpaVendorAdapter();
return hibernateJpaVendorAdapter;
  }

  @Bean("EntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() {
    LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean=new LocalContainerEntityManagerFactoryBean();
    localContainerEntityManagerFactoryBean.setDataSource(dataSource);
    localContainerEntityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter());
    Properties jpaProperties = new Properties();
    jpaProperties.setProperty("hibernate.dialect",hibernate_dialect);
    jpaProperties.setProperty("hibernate.show_sql",hibernate_show_sql);
    jpaProperties.setProperty("hibernate.format_sql",hibernate_format_sql);
    jpaProperties.setProperty("hibernate.hbm2ddl.auto",hibernate_hbm2ddl_auto);
    localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);
    localContainerEntityManagerFactoryBean.setPackagesToScan("com.my.bean");
    return localContainerEntityManagerFactoryBean;
  }
}
